﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
   public class listBatchSubscribeOutput : Api_BaseOutput
   {
      public listBatchSubscribeResults result { get; set; }
      public listBatchSubscribeParms inputParms { get; private set; }
      // --------------------------------------------------------
      public listBatchSubscribeOutput( listBatchSubscribeInput input )
         : base( input )
      {
         inputParms = input.parms;
         this.result = new listBatchSubscribeResults();
      }
      // --------------------------------------------------- tostring
      public override string ToString()
      {
         return string.Format( "Subscribed: {0}, Errors: {1}", result.success_count, result.error_count );
      }

      #region ================================================================== format setup

      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }
      // ---- xmlrpc
      internal void Format( x_listBatchSubscribe rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         this.result = FormatResults( rpcResult );
      }
      #endregion

      #region ================================================================= Output
      // ---------------------------------------------------------- outputXML
      protected void outputXML()
      {
         XElement xml = XElement.Parse( this.api_Response );
         // error check
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            this.api_ErrorMessages.Add( error );
            return;
         }
         // -------------------------------------------- get values
         try
         {
            int i;
            listBatchSubscribeResults results = new listBatchSubscribeResults();
            int.TryParse( xml.Element( "success_count" ).Value, out i ); results.success_count = i;
            int.TryParse( xml.Element( "error_count" ).Value, out i ); results.error_count = i;
            IEnumerable<XElement> errors = xml.Elements( "errors" ).Elements( "struct" );
            foreach ( XElement entry in errors )
            {
               listBatchSubscribeError err = new listBatchSubscribeError();
               int.TryParse( entry.Element( "code" ).Value, out i ); err.code = i;
               err.message = entry.Element( "message" ).Value;
               IEnumerable<XElement> rowVals = entry.Element( "row" ).Descendants();
               foreach ( XElement val in rowVals )
               {
                  err.row.Add( val.Name.LocalName, val.Value );
               }
               results.errors.Add( err );
            }
            this.result = results;
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }
      // ---------------------------------------------------------- outputJSON
      protected void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();

         try
         {
            if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
               this.api_ErrorMessages.Add( err );
            }
            else
            {
               x_listBatchSubscribe jsonResult = jss.Deserialize<x_listBatchSubscribe>( this.api_Response );
               this.result = FormatResults( jsonResult );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }
      // ---------------------------------------------------------- FormatReults
      private listBatchSubscribeResults FormatResults( x_listBatchSubscribe x_results )
      {
         listBatchSubscribeResults results = new listBatchSubscribeResults();
         //
         results.success_count = x_results.success_count;
         results.error_count = x_results.error_count;
         foreach ( x_listBatchSubscribeError err in x_results.errors )
         {
            listBatchSubscribeError apierr = new listBatchSubscribeError();
            apierr.code = err.code;
            apierr.message = err.message;
            //
            IDictionary dict = err.row as IDictionary;
            IDictionaryEnumerator ide = dict.GetEnumerator();
            while ( ide.MoveNext() )
            {
               apierr.row.Add( ide.Key.ToString(), ide.Value.ToString() );
            }
            //
            results.errors.Add( apierr );
         }
         //
         return results;
      }
      #endregion
   }
}
